package com.house.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.house.dto.BillDTO;
import com.house.entity.Bill;
import com.house.reportisy.BillRepository;
import com.house.service.BillService;
import com.house.service.RoomService;
import com.house.utils.BillNumberUtil;
import com.house.vo.BillVO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.*;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author: GGP
 * @description: 订单Service实现类
 * @time: 2023-04-16 11:29
 **/
@Service
public class BillServiceImpl implements BillService {

	@Resource
	private BillRepository billRepository;

	@Resource
	private RoomService roomService;

	@Override
	public Bill saveBill(BillDTO billDTO) {
		Bill bill = new Bill();
		BeanUtil.copyProperties(billDTO, bill, "number", "status", "id", "orderTime");
		bill.setBillNumber(BillNumberUtil.getBillNumber());
		bill.setStatus(1);
		bill.setScore(5);
		int dayCount = BillNumberUtil.getDayCount(billDTO.getCheckInTime(), billDTO.getCheckOutTime());
		bill.setPrice(billDTO.getPrice()*dayCount);
		System.out.println(bill.getPrice());
		return billRepository.save(bill);
	}

	@Override
	public Page<Map<String,Object>> findBills(BillDTO billDTO) {
		Integer page = billDTO.getPage();
		page = page==null||page<0? 0: page-1;
		Pageable pageable = PageRequest.of(page, billDTO.getSize(), Sort.Direction.DESC, "bill_time");
		return billRepository.findBills(billDTO,pageable);
	}

	@Override
	public BillVO findOne(Integer billNumber) {
		Optional<Bill> one = billRepository.findOne(new Specification<Bill>() {
			@Override
			public Predicate toPredicate(Root<Bill> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				// 动态 SQL表达式
				Predicate predicate = criteriaBuilder.conjunction();
				// 动态 SQL表达式集合
				List<Expression<Boolean>> expressions = predicate.getExpressions();
				expressions.add(criteriaBuilder.equal(root.get("billNumber"),billNumber));
				expressions.add(criteriaBuilder.equal(root.get("delFlag"), 0));
				return predicate;
			}
		});
		Bill bill = one.orElse(null);
		if (bill == null) {
			return null;
		}
		BillVO billVO = new BillVO();
		BeanUtil.copyProperties(bill, billVO);
		return billVO;
	}

	@Override
	public Bill update(BillDTO billDTO) {
		Optional<Bill> byId = billRepository.findById(billDTO.getId());
		Bill orderBill = byId.orElse(null);
		Bill newBill = new Bill();
		BeanUtil.copyProperties(orderBill, newBill);
		//修改状态
		if (billDTO.getStatus() != null) {
			//办理入住，分配房号，修改房号状态，修改订单状态
			if (billDTO.getStatus()==2){
				roomService.update(billDTO.getRoomId(),1);
				newBill.setRoomId(billDTO.getRoomId());
			}
			//订单已完成，修改订单状态,修改房间状态
			if (billDTO.getStatus()==4){
				roomService.update(billDTO.getRoomId(),0);
			}
			newBill.setStatus(billDTO.getStatus());
		}
		if (billDTO.getScore()!=null){
			newBill.setScore(billDTO.getScore());
			newBill.setStatus(5);
		}
		return billRepository.save(newBill);
	}

	@Override
	public Page<Map<String, Object>> findByUserId(BillDTO billDTO) {
		Integer page = billDTO.getPage();
		page = page==null||page<0? 0: page-1;
		Pageable pageable = PageRequest.of(page, billDTO.getSize(), Sort.Direction.DESC, "bill_time");
		return billRepository.findBillByUserId(billDTO.getUserId(), pageable);
	}

	@Override
	public String grossAmount(Integer publisherId) {
		return billRepository.grossAmount(publisherId);
	}

	@Override
	public String grossAmounts(Integer publisherId) {
		return billRepository.grossAmounts(publisherId);
	}


	@Override
	public Page<Map<String, Object>> findByRoomNum(BillDTO billDTO) {
		Integer page = billDTO.getPage();
		page = page==null||page<0? 0: page-1;
		Pageable pageable = PageRequest.of(page, billDTO.getSize(), Sort.Direction.DESC, "bill_time");
		return billRepository.findByRoomNum(billDTO, pageable);
	}

	@Override
	public Optional<Bill> findById(Integer id) {
		return billRepository.findById(id);
	}
}
